/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.router.core.repository.service;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import info.peigen.hotpot.business.router.core.domain.RouterTransLog;
import info.peigen.hotpot.business.router.core.domain.Withdraw;
import info.peigen.hotpot.business.router.core.domain.action.RouterWithdrawConverter;
import info.peigen.hotpot.business.router.core.repository.entity.RouterWithdrawEntity;
import info.peigen.hotpot.business.router.core.repository.mapper.RouterWithdrawMapper;
import info.peigen.hotpot.common.core.enums.Symbol;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

/**
 * <b>(RouterWithdrawRepository)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/8/5
 */
@Component
public class RouterWithdrawRepository extends AbstractBeetlSqlRepository<RouterWithdrawMapper, RouterWithdrawEntity> {
    @Inject
    RouterTransLogRepository routerTransLogRepository;

    public Withdraw active(String tradeId) {
        RouterWithdrawEntity entity   = findByTradeId(tradeId);
        Withdraw             withdraw = null;
        if (ObjectUtil.isNotNull(entity)) {
            withdraw = RouterWithdrawConverter.convert(entity);

            // 最初这些东西可能都没有，随着进程深入逐步填充，所以需要判断一下
            if (StrUtil.isNotEmpty(entity.getPositionTransLogId())) {
                RouterTransLog positionTransLog = routerTransLogRepository.active(entity.getPositionTransLogId());
                withdraw.setPositionTransLog(positionTransLog);
            }
            if (StrUtil.isNotEmpty(entity.getWithdrawTransLogIds())) {
                List<String> transLogIds = Lists.newArrayList();

                // 有可能只有一个，则不会被[逗号]分割了，提高健壮性
                if (StrUtil.contains(entity.getWithdrawTransLogIds(), Symbol.COMMA.code())) {
                    transLogIds.addAll(StrUtil.split(entity.getWithdrawTransLogIds(), Symbol.COMMA.code()));
                } else {
                    transLogIds.add(entity.getWithdrawTransLogIds());
                }

                List<RouterTransLog> withdrawTransLogs = routerTransLogRepository.active(transLogIds);
                withdraw.setWithdrawTransLogs(withdrawTransLogs);
            }
        }
        return withdraw;
    }

    public void store(Withdraw domain) {
        RouterWithdrawEntity entity = RouterWithdrawConverter.convert(domain);
        mapper.upsert(entity);
    }

    public RouterWithdrawEntity findByWithdrawId(String withdrawId) {
        return mapper.findByWithdrawId(withdrawId);
    }

    public RouterWithdrawEntity findByTradeId(String tradeId) {
        return mapper.findByTradeId(tradeId);
    }

    public RouterWithdrawEntity findByBatchNo(String batchNo) {
        return mapper.findByBatchNo(batchNo);
    }

    public RouterWithdrawEntity findByChannelUniId(String channelUniId) {
        return mapper.findByChannelUniId(channelUniId);
    }
}